<?php
/*
 * This file is part of the BooXtore Student project.
 *
 * (c) Francis Backeland f.backeland@gmail.com
 * (c) Laurine Chaplot chaplot.laurine@orange.fr
 * (c) Julien Dutot julien.dutot@gmail.com
 * (c) Romain Guillaume romain-guillaume@orange.fr
 * (c) Guillaume Paringaux g.paringaux@gmail.com
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code
 * 
 */
namespace BXTR\AdminBundle\Routing;

use Symfony\Component\Routing\Route;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\RouteCollection;

class ConfigLoader extends Loader
{
    protected $routeConfiguration;
    public function __construct($routeConfiguration)
    {
        $this->routeConfiguration = $routeConfiguration;
    }

    public function load($resource, $type = null)
    {
        $routes = new RouteCollection();
        
        $entities = $this->routeConfiguration;

        $routePrefix = 'bxtr_admin_';
        $pathPrefix = '/';
        $controllerBundle = 'BXTRAdminBundle';
        foreach($entities as $entity) {
            $routes->add($entity['routes']['add'], new Route($pathPrefix . $entity['route'] . '/add', array(
                '_controller' => $controllerBundle . ':' . $entity['controller'] . ':add',
                'entityName' => $entity['entity'],
                'tags' => array('tag.bxtr_admin.controller.entity'),
            )));
            $routes->add($entity['routes']['list'], new Route($pathPrefix . $entity['route'] . '/list/', array(
                '_controller' => $controllerBundle . ':' . $entity['controller'] . ':list',
                'entityName' => $entity['entity'],
                'tags' => array('tag.bxtr_admin.controller.entity'),
            )));
            $routes->add($entity['routes']['edit'], new Route($pathPrefix . $entity['route'] . '/edit/{id}', array(
                '_controller' => $controllerBundle . ':' . $entity['controller'] . ':edit',
                'entityName' => $entity['entity'],
                'tags' => array('tag.bxtr_admin.controller.entity'),
            )));
            $routes->add($entity['routes']['remove_popup'], new Route($pathPrefix . $entity['route'] . '/remove/{id}', array(
                '_controller' => $controllerBundle . ':' . $entity['controller'] . ':removePopup',
                'entityName' => $entity['entity'],
                'tags' => array('tag.bxtr_admin.controller.entity'),
            )));
        }
        return $routes;
    }

    public function supports($resource, $type = null)
    {
        return 'bxtr_admin_routing' === $type;
    }
}
